#include <iostream>
#include <vector>
#include <string>

using namespace std;

#include "dfx_log.h"
#include "unwinder.h"
#include "unwind.h"
#include <unwind.h> // GCC's internal unwinder, part of libgcc
_Unwind_Reason_Code trace_fcn(_Unwind_Context *ctx, void *d)
{
    int *depth = (int*)d;
    printf("\t#%d: program counter at %p\n", *depth, (void*)_Unwind_GetIP(ctx));
    (*depth)++;
    return _URC_NO_REASON;
}

void print_backtrace_here()
{
    int depth = 0;
    _Unwind_Backtrace(&trace_fcn, &depth);
}


int main()
{
   LOGE("Begin to unwind");
   auto unwinder = std::make_shared<OHOS::HiviewDFX::Unwinder>();
   if (!unwinder->UnwindLocal(false, 6, 0)){
      LOGE("Failed to unwind");
   } else {
      auto frames = unwinder->GetFrames();
      printf("Frame:%d\n", (int)frames.size());
      auto str = unwinder->GetFramesStr(frames);
      printf("%s\n", str.c_str());
   }
   LOGE("End to unwind");

   print_backtrace_here();

}